package cn.yqx.bankshop.oauth2.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;
import org.springframework.security.oauth2.provider.token.store.KeyStoreKeyFactory;

@Configuration
public class TokenConfig {

    private final static  String  OAUTH2_FILE_NAME = "oauth2.jks" ;
    private final static  String  OAUTH2_STR = "oauth2" ;
    @Autowired
    RedisConnectionFactory redisConnectionFactory ;

    @Bean
    public TokenStore getTokenStore(){
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
        KeyStoreKeyFactory factory = new KeyStoreKeyFactory(
                new ClassPathResource(OAUTH2_FILE_NAME), OAUTH2_STR.toCharArray());
        converter.setKeyPair(factory.getKeyPair(OAUTH2_STR));
        return converter ;
    }
}
